package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
@Autowired
    private BrandService brandService;

@GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> queryForPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "search", required = false)String search,
            @RequestParam(value = "sortBy", required = false)String sortBy,
            @RequestParam(value = "desc", defaultValue = "false")Boolean desc
    ){
        return ResponseEntity.ok(brandService.queryForPage(page,rows, search, sortBy, desc));
    }
@PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids ){
    brandService.saveBrand(brandDTO,cids);
    return ResponseEntity.status(HttpStatus.CREATED).body(null);

    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids ){
        brandService.updateBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);

    }
    @RequestMapping ("/delete")
    public ResponseEntity<Void> updateBrand( @RequestParam("id") Long id ){
        brandService.deleteById(id);
        return ResponseEntity.status(HttpStatus.CREATED).body(null);

    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(brandService.queryByCategoryId(cid));
    }

    @GetMapping("/{id}")
   public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
     return   ResponseEntity.ok(brandService.queryById(id));
    }
    @GetMapping("/brandIds")
   public ResponseEntity<List<BrandDTO>> queryByBrandIds(@RequestParam("ids") List<Long> ids){
        return   ResponseEntity.ok(brandService.queryByBrandIds(ids));
    }
}
